# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: leon

from coin.exchange.base.kr_rest.product import NativeProduct, NormProduct
from coin.exchange.bittrex_v1.constants import (quote_list, symbol_list)


def extract_quote_from_native_symbol(native_symbol):
  quote = native_symbol.split('-')[0]
  assert quote in quote_list
  return quote


class BittrexProduct(NativeProduct):
  _exchange = "Bittrex"

  def __init__(self, base, quote):
    assert type(quote) == str
    assert type(base) == str
    assert base == base.upper(), base
    assert quote == quote.upper(), quote
    assert quote in quote_list, quote
    NativeProduct.__init__(self, base, quote, "%s-%s" % (quote.upper(), base.upper()))
    assert self.native_symbol in symbol_list

  @classmethod
  def FromProduct(cls, product):
    assert isinstance(product, NormProduct), type(product)
    return BittrexProduct(product.base, product.quote)

  @classmethod
  def FromStr(cls, product):
    assert type(product) == str
    if "-" in product:
      exploded = product.split("-")
      assert len(exploded) == 2
      reverse_pair_product = "%s-%s" % (exploded[1], exploded[0])
      # For any currency pair in symbol list, make sure reverse base and quote
      # pair is not in the list, otherwise it is ambiguious.
      if product in symbol_list:
        assert reverse_pair_product not in symbol_list, \
            'Ambiguious product symbol! %s' % product
        return BittrexProduct(exploded[1], exploded[0])
      elif reverse_pair_product in symbol_list:
        return BittrexProduct(exploded[0], exploded[1])
    assert False, 'Invalid product %s' % product
